package cn.dida.service.impl;

import cn.dida.dao.CompanyDao;
import cn.dida.dao.DepartmentDao;
import cn.dida.exception.BizzException;
import cn.dida.model.Company;
import cn.dida.model.Department;
import cn.dida.service.DepartmentService;
import cn.dida.weixin.qyh.ErrorCodeMapping;
import cn.dida.weixin.qyh.QyhApiClient;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**
 * 部门管理 业务层接口实现类
 * @author Administrator
 *
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentDao departmentDao;
    @Autowired
    private CompanyDao companyDao;

    @Override

    public List<Department> getAllDepartment(Integer companyId) {
        return departmentDao.getAll(companyId);
    }
    @Transactional
    @Override
    public Boolean insertDepartment(Department department) {
        //录入微信企业号部门
        Company company = companyDao.get(department.getCompanyId());

        JSONObject preference = JSONObject.parseObject(company.getPreference());
        String corpid = preference.getString("weixin_qyh_corpid");
        String corpsecret = preference.getString("weixin_qyh_corpsecret");
        QyhApiClient qyhApiClient = new QyhApiClient(corpid, corpsecret);

        JSONObject re = qyhApiClient.createDepartment(department.getParentId(), department.getName());
        if (re.getInteger("errcode") == 0) {
            Integer id = re.getInteger("id");
            department.setId(id);
            return departmentDao.insert(department) == 1;
        } else {
            throw new BizzException(BizzException.error_code_invalid_request, ErrorCodeMapping.getMsgByCode(re.getInteger("errcode")));
        }

    }
    @Transactional
    @Override
    public Boolean insertDepartmentWithoutSync(Department department) {
        return departmentDao.insert(department) == 1;
    }
    @Transactional
    @Override
    public Boolean updateDepartment(Department department) {
        //更新微信企业号部门
        Company company = companyDao.get(department.getCompanyId());

        JSONObject preference = JSONObject.parseObject(company.getPreference());
        String corpid = preference.getString("weixin_qyh_corpid");
        String corpsecret = preference.getString("weixin_qyh_corpsecret");
        QyhApiClient qyhApiClient = new QyhApiClient(corpid, corpsecret);

        JSONObject re = qyhApiClient.updateDepartment(department.getId(), department.getParentId(), department.getName());
        if (re.getInteger("errcode") == 0) {
            return departmentDao.update(department) == 1;
        } else {
            throw new BizzException(BizzException.error_code_invalid_request, ErrorCodeMapping.getMsgByCode(re.getInteger("errcode")));
        }
    }

    @Override
    public Department getDepartment(Integer companyId, Integer id) {
        return departmentDao.get(companyId, id);
    }
    @Transactional
    @Override
    public Boolean deleteDepartment(Integer companyId, Integer id) {
        //删除微信企业号部门
        Company company = companyDao.get(companyId);
        JSONObject preference = JSONObject.parseObject(company.getPreference());
        String corpid = preference.getString("weixin_qyh_corpid");
        String corpsecret = preference.getString("weixin_qyh_corpsecret");
        QyhApiClient qyhApiClient = new QyhApiClient(corpid, corpsecret);

        JSONObject re = qyhApiClient.deleteDepartment(id);
        if (re.getInteger("errcode") == 0) {
            return departmentDao.delete(companyId, id) == 1;
        } else {
            throw new BizzException(BizzException.error_code_invalid_request, ErrorCodeMapping.getMsgByCode(re.getInteger("errcode")));
        }
    }
}
